package com.yeebo.service.config;

import com.github.pagehelper.Page;
import com.yeebo.domain.config.GlobalParamDomain;
import com.yeebo.enums.ResultCode;
import com.yeebo.exception.ServiceException;
import com.yeebo.model.config.GlobalParam;
import com.yeebo.rpc.config.GlobalParamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 全局变量服务实现类
 */
@Service
public class GlobalParamServiceImpl implements GlobalParamService {

    @Resource
    private GlobalParamDomain globalParamDomain;

    @Override
    public void addGlobalParam(String paramName, String paramValue, String paramDesc) {
        GlobalParam checkExistRecord = globalParamDomain.findByParamName(paramName);
        if (checkExistRecord != null) {
            throw new ServiceException(ResultCode.EXIST_SAME_GLOBAL_PARAM);
        } else {
            checkExistRecord = new GlobalParam();
            checkExistRecord.setParamName(paramName);
            checkExistRecord.setParamValue(paramValue);
            checkExistRecord.setParamDesc(paramDesc);
            checkExistRecord.setCreatedTime(new Date());
            checkExistRecord.setUpdatedTime(new Date());
            globalParamDomain.addGlobalParam(checkExistRecord);
        }
    }

    @Override
    public void updateGlobalParam(String paramName, String paramValue, String paramDesc) {
        GlobalParam updateRecord = globalParamDomain.findByParamName(paramName);
        updateRecord.setParamValue(paramValue);
        updateRecord.setParamDesc(paramDesc);
        globalParamDomain.updateGlobalParam(updateRecord);
    }

    @Override
    public Page<GlobalParam> loadGlobalParamList(String filterParamDesc, Integer pageNo, Integer pageSize) {
        return globalParamDomain.loadGlobalParamList(filterParamDesc, pageNo, pageSize);
    }

}
